import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import styles from './style.css';
import {Card, Button, Row, Col} from 'antd';
import {PERMISSION} from './constants';
import routes from './routes';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
const {Description} = DescriptionList;
const EMPTY_ARRAY = [];
/*eslint-disable  react/prefer-stateless-function*/
export class OrderInfo extends React.PureComponent {
    render() {
        const operations = [];
        if(this.props.isEditable)
            operations.push(
                <Button key="update" href={`#${routes.update.format(this.props.id)}`} className={styles.btnMargin} size="small">
                    {this.props.intl.formatMessage({
                        id: 'detail.btn.update',
                        defaultMessage: '编辑'
                    })}
                </Button>
            );
        return (
            <div className={styles.marginBottom}>
                <Card>
                    <DescriptionList
                        size="large"
                        title={<Row className={styles.marginBottom}>
                            <Col span={6}>
                                {this.props.intl.formatMessage({
                                    id: 'orderInfo.card.title',
                                    defaultMessage: '订单类型信息'
                                })}
                            </Col>
                            <Col className={styles.textAlign} span={18}>
                                {operations}
                            </Col>
                        </Row>}>
                        <Description
                            term={this.props.intl.formatMessage({
                                id: 'orderInfo.info.typeName',
                                defaultMessage: '订单类型'
                            })}>
                            {this.props.data.typeName}
                        </Description>
                        <Description
                            term={this.props.intl.formatMessage({
                                id: 'orderInfo.info.creditCode',
                                defaultMessage: '销售类型凭证编号'
                            })}>
                            {this.props.data.creditCode}
                        </Description>
                        <Description
                            term={this.props.intl.formatMessage({
                                id: 'orderInfo.info.orderReasonCode',
                                defaultMessage: '订购原因编号'
                            })}>
                            {this.props.data.orderReasonCode}
                        </Description>
                    </DescriptionList>
                    <DescriptionList col={1} size="large">
                        <Description
                            term={this.props.intl.formatMessage({
                                id: 'orderInfo.info.orderReason',
                                defaultMessage: '订购原因描述'
                            })}>
                            {this.props.data.orderReason}
                        </Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button type="primary" href={`#${routes.query.url()}`}>
                        {this.props.intl.formatMessage({
                            id: 'orderInfo.btn.return',
                            defaultMessage: '返回'
                        })}
                    </Button>
                </div>
            </div>
           

        );
    }
}
OrderInfo.propTypes = {
    data: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    isEditable: PropTypes.bool.isRequired,
    loading: PropTypes.bool.isRequired,
    intl: PropTypes.object
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const EMPTY_OBJ = {};
const getData = selectorFactory(['page', 'domainData', 'initData', 'saleOrderTypes']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getOrder = createSelector(
    state => getData(state),
    (state, ownProps) => ownProps.id,
    (types, id) => types.find(item => item.id === id) || EMPTY_OBJ
);
const getEditable = createSelector(
    state => getPermissions(state),
    (state, ownProps) => getOrder(state, ownProps),
    (state, ownProps) => ownProps.isEdit,
    (permissions, data, editable) => {
        const {options = EMPTY_ARRAY} = data;
        return options.includes(PERMISSION.update) && permissions.includes(PERMISSION.update) && Boolean(editable);
    }
);
const mapStateToProps = (state, ownProps) => ({
    loading: state.getIn(['page', 'domainData', 'initData', 'isFetching']),
    data: getOrder(state, ownProps),
    isEditable: getEditable(state, ownProps),
});

export default connect(
    mapStateToProps,
    null
)(injectIntl(OrderInfo));
